package cn.px.rest.controller.purchase;

import cn.px.common.util.R;
import cn.px.dao.dto.purchase.PurchaseContractDTO;
import cn.px.dao.entity.purchase.PurchaseContract;
import cn.px.service.purchase.PurchaseContractService;
import cn.px.dao.util.ExcelUtil;
import cn.px.dao.vo.export.purchase.ExportPurchaseContractVO;
import cn.px.dao.vo.query.purchase.PurchaseContractVO;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.validation.group.AddGroup;
import cn.px.common.validation.group.UpdateGroup;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;
import cn.px.common.bean.AbstractPageResultBean;
import cn.px.rest.log.annotation.SysLog;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description 采购合同 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/purchase/purchase-contracts")
public class PurchaseContractController {

    private final PurchaseContractService purchaseContractService;

    /**
     * 通过id查询单条记录
     *
     * @param id 主键
     * @return R
     */
    @GetMapping("/{id}")
    public R<PurchaseContract> getById(@PathVariable("id") String id) {
        return new R<>(purchaseContractService.getModelById(id));
    }

    /**
     * 新增记录
     *
     * @param purchaseContractDTO 采购合同DTO
     * @return R
     */
    @SysLog("新增采购合同")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('purchase-purchase-contracts-add')")
    public R save(@Validated({AddGroup.class}) @RequestBody PurchaseContractDTO purchaseContractDTO) {
        return new R<>(purchaseContractService.saveModelByDTO(purchaseContractDTO));
    }

    /**
     * 修改记录
     *
     * @param purchaseContractDTO 采购合同DTO
     * @return R
     */
    @SysLog("修改采购合同")
    @PutMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('purchase-purchase-contracts-edit')")
    public R update(@PathVariable("id") String id, @Validated({UpdateGroup.class}) @RequestBody PurchaseContractDTO purchaseContractDTO) {
        purchaseContractService.modModelByDTO(purchaseContractDTO);
        return new R<>();
    }

    /**
     * 修改记录
     *
     * @param purchaseContractDTO 采购合同DTO
     * @return R
     */
    @SysLog("上传采购合同")
    @PutMapping("/{id}/upload")
    @PreAuthorize("@pms.hasPermission('purchase-purchase-contracts-edit')")
    public R upload(@PathVariable("id") String id, @Validated({UpdateGroup.class}) @RequestBody PurchaseContractDTO purchaseContractDTO) {
        purchaseContractService.uploadContract(purchaseContractDTO);
        return new R<>();
    }

    /**
     * 通过id删除一条记录
     *
     * @param id 主键
     * @return R
     */
    @SysLog("删除采购合同")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('purchase-purchase-contracts-del')")
    public R removeById(@PathVariable String id) {
        purchaseContractService.deleteByPrimaryKey(id);
        return new R<>();
    }

    /**
     * 批量记录
     *
     * @param ids 主键
     * @return R
     */
    @SysLog("批量删除采购合同")
    @DeleteMapping("")
    @PreAuthorize("@pms.hasPermission('purchase-purchase-contracts-del')")
    public R removeIds(@RequestBody List<String> ids) {
        purchaseContractService.batchDelete(ids);
        return new R<>();
    }

    /**
     * 获取分页
     *
     * @param pageNum             当前页码
     * @param pageSize            每页条数
     * @param purchaseContractDTO 采购合同DTO
     * @return R
     */
    @GetMapping("/page")
    public R<AbstractPageResultBean<PurchaseContractVO>> query(PurchaseContractDTO purchaseContractDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
        return new R<>(purchaseContractService.selectPageList(purchaseContractDTO, pageNum, pageSize));
    }

    /**
     * 获取列表
     *
     * @param purchaseContractDTO 采购合同DTO
     * @return R
     */
    @GetMapping("")
    public R query(PurchaseContractDTO purchaseContractDTO) {
        return new R<>(purchaseContractService.getModelListByDTO(purchaseContractDTO));
    }

    /**
     * 采购合同导出
     *
     * @param purchaseContractDTO 采购合同DTO
     * @return R
     */
    @GetMapping("/export")
    @SysLog("采购合同导出")
    @PreAuthorize("@pms.hasPermission('purchase-purchase-contracts-export')")
    public void export(HttpServletResponse response, PurchaseContractDTO purchaseContractDTO) {
        List<PurchaseContract> list = purchaseContractService.getModelListByDTO(purchaseContractDTO);
        ExcelUtil.exportResponseDict(response, ExportPurchaseContractVO.class, BeanConverterUtil.copyListProperties(list, ExportPurchaseContractVO.class), "采购合同");
    }

}
